// pages/user/user.js
const app = getApp()
const api = require("../../config/api.js")
Page({

  /**
   * 页面的初始数据
   */
  data: {
    activeNames: [],
    users: null,
    user: null,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (app.globalData.user != null) {
      this.setData({
        user: app.globalData.user
      })
    }
    console.log(this.data.user)
    var that = this
    wx.request({
      url: api.findUsers,
      method: "GET",
      success(res) {
        var list = res.data.data
        var list1 = []
        for (let user of list) {
          list1.push(user.userId)
        }
        that.setData({
          users: res.data.data,
          activeNames: list1
        })
        console.log(that.data.users)
      }
    });
    wx.stopPullDownRefresh()
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },


  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.onLoad()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  onChange(event) {
    // console.log(event.detail)
    this.setData({
      activeNames: event.detail,
    });
  },
  setAdmin(e) {
    console.log(e.currentTarget.dataset.item)
    var role = null
    var that = this
    if (e.currentTarget.dataset.item.role == 2) {
      role = 1
    } else(role = 2)
    wx.showModal({
      title: '提示',
      content: '您确定要设其为管理吗',
      success(res) {
        if (res.confirm) {
          wx.request({
            url: api.setAdmin + "?userId=" + e.currentTarget.dataset.item.userId + "&&role=" + role,
            method: "GET",
            success(result) {
              wx.showToast({
                title: '操作成功',
              })
              that.onLoad()
            }
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  }
})